package dmqm.mall.service.impl;

import dmqm.mall.mapper.ProductMapper;
import dmqm.mall.model.Product;
import dmqm.mall.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@CacheConfig(cacheNames = "product")
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    @Cacheable
    public List List() {
        return productMapper.List();
    }

    @Override

    public boolean Add(Product product) {
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        return productMapper.Add(product) > 0;
    }

    @Override
    @CacheEvict
    public boolean Update(Product product) {
        product.setUpdateTime(new Date());
        return productMapper.Update(product) > 0;
    }

    @Override
    @CacheEvict
    public boolean Delete(int id) {
        return productMapper.Delete(id) > 0;
    }

    @Override
    @Cacheable
    public Product Get(int id) {
        return productMapper.Get(id);
    }
}
